﻿# Add Sharepoint pssnapin

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

# Set up a bunch of useful aliases...

#Set-Alias psconfig ((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0').Location + "Bin\psconfig.exe")
Set-Alias stsadm ((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0').Location + "Bin\stsadm.exe")


function addCA(){

## Add http://farm:8888 to Central Admin
		
$existcheck = $false
$publicURL = $farmurl
Get-SPAlternateURL | % { 
		if ($_.IncomingURL -eq "$($env:farmurl):8888"){
		$existcheck = $true
		} 
		if ($_.IncomingURL -like "*8888*"){
		$publicurl = $_.PublicUrl
		}

	}

	if (!$existcheck){

	New-SPAlternateURL -url "$($env:farmurl):8888" -zone default -webapplication "$publicURL" -internal

		if( !$? )
		{
			Write-Error "New-SPAlternateURL -url $($env:farmurl):8888 -zone default -webapplication $publicURL -internal failed"
		}
	}

}

#bug #fix for multiple web applications
$webapps = get-spwebapplication |% { $_.Url }

	foreach ($webapp in $webapps){
	
	Write-Host -ForegroundColor Yellow "AAM: Setting up Alternate Access Mapping for $webapp."
	
		$servers=Get-SPServer
		foreach ( $server in $servers)
		{
			if( $server.Role -eq "WebFrontEnd" -OR $server.Role -eq "Application" ){
				
				$server = $server.Name
				
				#ISEG- MSIT START ##
				$existcheck = $false
			
				Get-SPAlternateURL | % { 
					if ($_.IncomingURL -eq "http://$server"){
					$existcheck = $true
					} 
				}
				
			
				if (!$existcheck){
			
				New-SPAlternateURL -url "http://$server" -zone default -webapplication "$webapp" -internal
				if( !$? )
				{
					Write-Error "Failed to add internal url http://$server to web application $webapp"
				}
			
				}

			#ISEG- MSIT END ##
			
			}
		}
		
	Write-Host -ForegroundColor Yellow "AAM: Setting up Alternate Access Mapping for $webapp. : COMPLETED"
	}

## Add Central Admin to AAM
addCA

if ($webapps -gt 1){
## stop if there are multiple webapps
exit 0
}
		
## Add http://localhost for the incoming URL		
$existcheck = $false
Get-SPAlternateURL | % { 
		if ($_.IncomingURL -eq "http://localhost" -and $_.PublicURL -eq "$env:farmurl"){
		$existcheck = $true
		} 

	}

	if (!$existcheck){

	New-SPAlternateURL -url "http://localhost" -zone default -webapplication "$env:farmurl" -internal

		if( !$? )
		{
			Write-Error "New-SPAlternateURL -url "http://localhost" -zone default -webapplication "$env:farmurl" -internal failed"
		}
	}




